package com.wyz.easyexcel.configuration;

import com.wyz.easyexcel.constant.I18nConstant;
import com.wyz.easyexcel.constant.RequestConstant;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;

import java.util.List;
import java.util.Locale;

/**
 * @author wyz
 * @description: i18n MessageSource 配置类
 * @date 2021/12/29 17:54
 */

@Data
@Configuration
@ConfigurationProperties(prefix = I18nConstant.I18N_PROPERTIES_PREFIX)
public class MessageSourceConfiguration {
    /**
     *  I18n 国际化所在地址
     */
    private List<String> i18nLocationList = List.of(I18nConstant.DEFAULT_I18N_LOCATION);

    /**
     *  缓存时间
     */
    private int cacheSeconds = I18nConstant.DEFAULT_CACHE_SECONDS;

    /**
     *  默认字符集
     */
    private String defaultEncoding = RequestConstant.FILE_ENCODING;

    /**
     *  默认中文
     */
    private Locale defaultLocale = Locale.CHINA;

    @Bean
    public MessageSource messageSource(){
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        String[]  baseNames =  new String[i18nLocationList.size()];
        messageSource.setBasenames(i18nLocationList.toArray(baseNames));
        messageSource.setDefaultEncoding(defaultEncoding);
        messageSource.setCacheSeconds(cacheSeconds);
        return messageSource;
    }

}
